package com.xingxuecn.limit.service.impl;

import com.xingxuecn.limit.common.model.Menu;
import com.xingxuecn.limit.common.model.Page;
import com.xingxuecn.limit.common.model.TreeNode;
import com.xingxuecn.limit.dao.MenuDao;
import com.xingxuecn.limit.dao.impl.MenuDaoImpl;
import com.xingxuecn.limit.service.MenuService;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Kris at 2018/9/5 18:00
 */
public class MenuServiceImpl implements MenuService {
    private MenuDao menuDao = new MenuDaoImpl();

    @Override
    public List<Menu> findAll() throws SQLException {
        return menuDao.findAll();
    }

    @Override
    public List<Menu> search(Menu menu) throws SQLException {
        return menuDao.search(menu);
    }

    @Override
    public int add(Menu menu) throws SQLException {
        return menuDao.add(menu);
    }

    @Override
    public int update(Menu menu) throws SQLException {
        return menuDao.update(menu);
    }

    @Override
    public int delete(Integer id) throws SQLException {
        return menuDao.delete(id);
    }

    @Override
    public List<TreeNode> menuTree() throws SQLException {
        List<Menu> menuList = menuDao.findAll();
        //转成treeNode
        List<TreeNode> treeNodeList = new LinkedList<>();
        for (Menu menu : menuList) {
            TreeNode treeNode = new TreeNode(menu.getId(),menu.getParent_id(),menu.getName(),menu.getUrl(),null);
            treeNodeList.add(treeNode);
        }

        List<TreeNode> result = TreeNode.buildTree(treeNodeList);
        return result;
    }

    @Override
    public Page searchPage(Menu menu, Integer page, Integer rows) throws SQLException {
        return menuDao.searchPage(menu, page, rows);
    }
}
